<?php
/**
 * 冰川时代5海报上传
 */
class Action
{
    /**
     * 处理登陆
     * @return json
     */
    public function login() {
        $username = an_request('username', '');
        $password = an_request('password', '');

        //数据必填及格式校验
        $this->login_vadation($username, $password);

        //验证用户名密码
        $parent_id = user_helper::get_user_id_by_password($username, $password);
        if (empty($parent_id)) {
            return_data(1, '用户名不存在或密码错误');
        }

        $userinfo = user_helper::get_user_info($parent_id);
        //判断是否为合伙人或市场人员
        if (!in_array($userinfo['role_type'], array(user_config::AGENCY, user_config::MARKETING))) {
            return_data(1, '您无权限登陆');
        }

        $_SESSION['user_role_id'] = intval($userinfo['role_type']);
        $_SESSION['user_id'] = $userinfo['id'];

        return_data(0, 'success');
    }

    /**
     * 任务上传页
     * @return json
     */
    public function index() {
        $module_path = '/module/glacial_era';
        Response::assign('module_path', $module_path);

        if (!isset($_SESSION['user_id']) || empty($_SESSION['user_id'])) {
            Response::display('login.html');
            return false;
        }

        $parent_id = $_SESSION['user_id'];
        switch ($_SESSION['user_role_id']) {
            case user_config::AGENCY:
                $store_list = _model('store')->field('id', 'title', 'title_branch')->getList(array('agency_id' => $parent_id));
                break;
            case user_config::MARKETING:
                $store_list = _model('store')->field('id', 'title', 'title_branch')->getList(array('marketing_id' => $parent_id));
                break;
        }
        foreach ($store_list as $key => $value) {
            $store_list[$key]['title'] = $value['title'] . '-' . $value['title_branch'];
            unset($store_list[$key]['title_branch']);
        }

        $realname = user_helper::get_user_info($parent_id)['realname'];

        Response::assign('realname', $realname);
        Response::assign('parent_id', $parent_id);
        Response::assign('store_list', json_encode($store_list));
        Response::display('task.html');
    }

    //处理海报上传
    public function upload() {
        $module_path = '/module/glacial_era';
        Response::assign('module_path', $module_path);

        if (!isset($_SESSION['user_id']) || empty($_SESSION['user_id'])) {
            Response::display('login.html');
            return false;
        }

        $parent_id = $_SESSION['user_id'];
        $store_id = an_request('store_id', 0);
        $image = an_request('image', '');
        $image_data = an_request('image_data', '');

        //数据必填校验
        $this->upload_vadation($store_id, $image, $image_data);

        //将jpeg的扩展名改成jpg
        $extname = pathinfo($image, PATHINFO_EXTENSION);
        $extname = $extname == 'jpeg' ? 'jpg' : $extname;

        $file_name = md5($image . datetime()) . '.' . $extname;
        //图片上传
        file_put_contents('/tmp/' . $file_name, base64_decode($image_data));
        if (!ONDEV) {
            $file_path = aliysso_helper::sso_upload_file($file_name, '/tmp/' . $file_name);
        } else {
            $file_path = move_image($file_name);
        }

        $info = array();
        if (!$file_path) {
            $info = array('flag' => 1, 'message' => '文件上传失败');
        } else {
            $table_name = 'topic_glacial_era';
            $data = array(
                'parent_id' => $parent_id,
                'store_id' => $store_id,
                'src' => $file_path,
                'status' => 0, //重新上传时把状态改为待审核
                'upload_time' => datetime() //重新上传时更新上传时间
            );

            $res = _model($table_name)->read(array('store_id' => $store_id, 'parent_id' => $parent_id));
            if (!$res) {
                _model($table_name)->create($data);
            } else {
                if ($res['status'] == 1) {
                    $info = array('flag' => 1, 'message' => '此图片已审核通过, 不允许再次上传');
                } else {
                    _model($table_name)->update($res['id'], $data);
                }
            }

            if (!$info) {
                $info = array('flag' => 1, 'message' => '上传成功');
            }
        }

        Response::assign('info', $info);
        Response::display('result.html');
    }

    /**
     * 退出登陆
     * @return json
     */
    public function logout() {
        if (isset($_SESSION['user_id'])) {
            unset($_SESSION['user_id']);
            unset($_SESSION['user_role_id']);
        }

        Response::redirect('glacial_era');
    }

    /**
     * 登陆验证
     * @param string $username 用户名
     * @param string $password 密码
     * @return json
     */
    private function login_vadation($username, $password) {
        if (!$username || !user_helper::is_mobile($username)) {
            return_data(1, '手机号格式不正确');
        }
        if (!$password) {
            return_data(1, '请输入密码');
        }
    }

    private function upload_vadation($store_id, $image, $image_data) {
        if (!$store_id) {
            return_data(1, '请选择门店');
        }
        if (!$image || !$image_data) {
            return_data(1, '请上传图片');
        }
    }
}


?>